﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VNet.VisiSuite.Common.Logger;

namespace VNet.VisiSuite.Common.Helper
{
    public static class RegistryHelper
    {
        public static bool CheckRegistryInstallKeyExist()
        {
            var reg = Registry.CurrentUser.OpenSubKey(CommonConstants.RegistrySoftware, false);
            if (reg == null) return false;
            reg = reg.OpenSubKey(CommonConstants.RegistryLeanlogic, false);
            if (reg == null) return false;
            reg = reg.OpenSubKey(CommonConstants.RegistryVisiSuite, false);
            if (reg == null) return false;

            string key = reg.GetValue(CommonConstants.RegistryInstallKey, string.Empty).ToString();
            string url = reg.GetValue(CommonConstants.RegistryServiceUrl, string.Empty).ToString();
            reg.Close();

            if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(key))
                return false;

            return true;
        }

        public static bool CheckRegistryPasswordExist(string registryVisi)
        {
            var reg = Registry.CurrentUser.OpenSubKey(CommonConstants.RegistrySoftware, false);
            if (reg == null) return false;
            reg = reg.OpenSubKey(CommonConstants.RegistryLeanlogic, false);
            if (reg == null) return false;
            reg = reg.OpenSubKey(registryVisi, false);
            if (reg == null) return false;

            string userName = reg.GetValue(CommonConstants.RegistryUserName, string.Empty).ToString();
            string password = reg.GetValue(CommonConstants.RegistryPassword, string.Empty).ToString();
            reg.Close();

            if (string.IsNullOrEmpty(password) || string.IsNullOrEmpty(userName))
                return false;

            return true;
        }

        public static bool CheckRegistryVisiExist(string registryVisi)
        {
            var reg = Registry.CurrentUser.OpenSubKey(CommonConstants.RegistrySoftware, false);
            if (reg == null) return false;
            reg = reg.OpenSubKey(CommonConstants.RegistryLeanlogic, false);
            if (reg == null) return false;
            reg = reg.OpenSubKey(registryVisi, false);
            if (reg == null) return false;

            return true;
        }

        public static void DeleteInstallKeyRegistry()
        {
            if (RegistryHelper.CheckRegistryVisiExist(CommonConstants.RegistryVisiSuite))
            {
                var reg = Registry.CurrentUser.OpenSubKey(CommonConstants.RegistryFullPath + CommonConstants.RegistryVisiSuite, true);
                try
                {
                    reg.DeleteValue(CommonConstants.RegistryInstallKey);
                    reg.DeleteValue(CommonConstants.RegistryServiceUrl);
                }
                catch (Exception ex)
                {
                    FileLog.Error("DeleteInstallKeyRegistry() - ", ex);
                }
                reg.Close();
            }
        }

        public static RegistryKey OpenRegistryVisi(string registryVisi, bool writable)
        {
            RegistryKey reg;
            if (!RegistryHelper.CheckRegistryVisiExist(registryVisi))
            {
                reg = Registry.CurrentUser.OpenSubKey(CommonConstants.RegistrySoftware, writable);
                reg = reg.CreateSubKey(CommonConstants.RegistryLeanlogic).CreateSubKey(registryVisi);
            }
            else
                reg = Registry.CurrentUser.OpenSubKey(CommonConstants.RegistryFullPath + registryVisi, writable);
            return reg;
        }

        public static bool isRunAtStartup()
        {
            int val = 0;

            try
            {
                var reg = Registry.CurrentUser.OpenSubKey(CommonConstants.RegistrySoftware, false);
                if (reg == null) return false;
                reg = reg.OpenSubKey(CommonConstants.RegistryLeanlogic, false);
                if (reg == null) return false;
                reg = reg.OpenSubKey(CommonConstants.RegistryVisiSuite, false);
                if (reg == null) return false;

                val = Convert.ToInt32(reg.GetValue(CommonConstants.RegistryRunAtStartUp, 0));
                reg.Close();

            }
            catch (Exception ex)
            {
                FileLog.Error("isRunAtStartup() - ", ex);
                val = 0;
            }

            return (val != 0);
        }

    }
}
